//
//  ViewController.m
//  WJAgroManagerDemo
//
//  Created by tqh on 2020/3/9.
//  Copyright © 2020 tqh. All rights reserved.
//

#import "ViewController.h"
#import "WJGiftGroupView.h"

@interface ViewController ()

@property (nonatomic,strong) WJGiftGroupView *groupView;

@end

@implementation ViewController

- (void)dealloc {
    NSLog(@"销毁了~~~");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton new];
        button.frame = CGRectMake(0, 100 + 40 *i, 150, 30);
        button.backgroundColor = [UIColor redColor];
        [button setTitle:[NSString stringWithFormat:@"发送礼物%d",i+11] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i+1;
        [self.view addSubview:button];
    }

    [self.view addSubview:self.groupView];
    self.groupView.frame = CGRectMake(0, 300, 300, 100);
    self.groupView.backgroundColor = [UIColor blackColor];
    
    [self createTimer];
    
}
- (void)presentView {
    ViewController *vc = [[ViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)presentView1 {
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark -

- (void)buttonPressed:(UIButton *)sender {
    NSInteger index = sender.tag + 10;
    
    WJGiftGroupItemModel *model = [WJGiftGroupItemModel new];
    
    if (index == 11) {
        model.giftName = @"礼物11";
        model.giftNumber = 1;
        model.nickName = @"小明";
        model.userId = 1;
        [self.groupView addGiftWithModel:model];
    }else  if (index == 12){
        model.giftName = @"礼物12";
        model.giftNumber = 1;
        model.nickName = @"小明";
        model.userId = 1;
        [self.groupView addGiftWithModel:model];
    }else {
        model.userId = 2;
        model.giftName = @"礼物12";
        model.nickName = @"小红";
        model.giftNumber = 1;
        [self.groupView addGiftWithModel:model];
    }
    
    //-----
    
//    [self.groupView addGift];
    
}

//弹出界面全部消失
- (void)wj_dismiss {
    UIViewController *currentVC = [self wj_findVisibleViewController];
    UIViewController *vcPresentVC = currentVC.presentingViewController;
     if (vcPresentVC) {
         while (vcPresentVC.presentingViewController)  {
             vcPresentVC = vcPresentVC.presentingViewController;
         }
         [vcPresentVC dismissViewControllerAnimated:YES completion:nil];
     }
}

//获取根控制器
- (UIViewController *)wj_getRootViewController{
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    return window.rootViewController;
}

//找到最顶层控制器
- (UIViewController *)wj_findVisibleViewController {
    UIViewController* currentViewController = [self wj_getRootViewController];
    BOOL runLoopFind = YES;
    while (runLoopFind) {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            } else if ([currentViewController isKindOfClass:[UISplitViewController class]]) { // 当需要兼容 Ipad 时
                currentViewController = currentViewController.presentedViewController;
            } else {
                if (currentViewController.presentedViewController) {
                    currentViewController = currentViewController.presentedViewController;
                     NSLog(@"%@",[currentViewController class]);
                } else {
                    return currentViewController;
                }
            }
    }
    return currentViewController;
}




- (void)createTimer {
    /**
     *   GCD 计时器应用
     *   dispatch Queue :决定了将来回调的方法在哪里执行。
     *   dispatch_source_t timer  是一个OC对象
     *   DISPATCH_TIME_NOW  第二个参数：定时器开始时间,也可以使用如下的方法，在Now 的时间基础上再延时多长时间执行以下任务。

     dispatch_time(<#dispatch_time_t when#>, <#int64_t delta#>)
     
     *   intervalInSeconds  第三个参数:定时器开始后的间隔时间（纳秒 NSEC_PER_SEC）
     *  leewayInSeconds 第四个参数：间隔精准度，0代标最精准，传入一个大于0的数，代表多少秒的范围是可以接收的,主要为了提高程序性能，积攒一定的时间，Runloop执行完任务会睡觉，这个方法让他多睡一会，积攒时间，任务也就相应多了一点，而后一起执行
     */

     // 全局队列
    dispatch_queue_t  queue = dispatch_get_global_queue(0, 0);

    // 创建一个 timer 类型定时器 （ DISPATCH_SOURCE_TYPE_TIMER）
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

    //设置定时器的各种属性（何时开始，间隔多久执行）
    // GCD 的时间参数一般为纳秒 （1 秒 = 10 的 9 次方 纳秒）
    // 指定定时器开始的时间和间隔的时间
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC, 0);

    // 任务回调
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"-----定时器-------");
        [self.groupView updateGiftTime];
    });

    // 开始定时器任务（定时器默认开始是暂停的，需要复位开启）
    dispatch_resume(timer);
}


#pragma mark - 懒加载

- (WJGiftGroupView *)groupView {
    if (!_groupView) {
        _groupView = [[WJGiftGroupView alloc]init];
    }
    return _groupView;
}

@end
